<?php
//   +----------------------------------------------------------------------
//   | Copyright (c) 2015-2025 http://www.hdphp.cn All rights reserved.
//   | Licensed ( http://www.hdphp.cn/licenses/ )
//   | Author: Jack <sophia2152@qq.com>
//   | 官方网址: http://www.hdphp.cn
//   | 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用。
//   | 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
//   +----------------------------------------------------------------------
namespace app\services\system\attachment;

use app\services\BaseServices;
use app\dao\system\attachment\SystemAttachmentCategoryDao;
use hdphp\exceptions\AdminException;

/**
 * Class：系统附件分类服务
 * 创建人：Jack
 * 创建时间：2023/7/28 17:41
 * @package app\services\system\attachment
 * @mixin SystemAttachmentCategoryDao
 */
class SystemAttachmentCategoryServices extends BaseServices
{

    /**
     * SystemAttachmentCategoryServices constructor.
     * @param SystemAttachmentCategoryDao $dao
     */
    public function __construct(SystemAttachmentCategoryDao $dao)
    {
        $this->dao = $dao;
    }

    /**
     * 注释：获取分类列表
     * 创建人：Jack
     * 创建时间：2023/8/23 15:38
     * @param array $where
     * @return array
     */
    public function getAll(array $where)
    {
        $list = $this->dao->getList($where);
        if ($where['all'] == 1) {
            $list = $this->tidyMenuTier($list);
        } else {
            foreach ($list as &$item) {
                $item['title'] = $item['name'];
                if ($where['name'] == '' && $this->dao->count(['pid' => $item['id']])) {
                    $item['loading'] = false;
                    $item['children'] = [];
                }
            }
        }

        return compact('list');
    }

    /**
     * 格式化列表
     * @param       $menusList
     * @param int   $pid
     * @param array $navList
     * @return array
     */
    public function tidyMenuTier($menusList, $pid = 0, $navList = [])
    {
        foreach ($menusList as $k => $menu) {
            $menu['title'] = $menu['name'];
            if ($menu['pid'] == $pid) {
                unset($menusList[$k]);
                $menu['children'] = $this->tidyMenuTier($menusList, $menu['id']);
                if ($menu['children']) $menu['expand'] = true;
                $navList[] = $menu;
            }
        }
        return $navList;
    }

    /**
     * 获取分类列表（添加修改）
     * @param array $where
     * @return mixed
     */
    public function getCateList(array $where)
    {
        $list = $this->dao->getList($where);
        $options = [['value' => 0, 'label' => '所有分类']];
        foreach ($list as $id => $cateName) {
            $options[] = ['label' => $cateName['name'], 'value' => $cateName['id']];
        }
        return $options;
    }

    /**
     * 注释：创建保存分类
     * 创建人：Jack
     * 创建时间：2023/8/23 15:43
     * @param array $data
     * @return mixed
     */
    public function save(array $data)
    {
        if ($this->dao->getOne(['name' => $data['name']])) {
            throw new AdminException('该分类已经存在!');
        }
        $res = $this->dao->save($data);
        if (!$res) throw new AdminException('新增失败!');
        return $res;
    }

    /**
     * 注释：保存修改分类
     * 创建人：Jack
     * 创建时间：2023/8/23 15:45
     * @param int   $id
     * @param array $data
     * @return void
     */
    public function update(int $id, array $data)
    {
        $attachment = $this->dao->getOne(['name' => $data['name']]);
        if ($attachment && $attachment['id'] != $id) {
            throw new AdminException('该分类已经存在!');
        }
        $res = $this->dao->update($id, $data);
        if (!$res) throw new AdminException('编辑失败!');
    }

    /**
     * 注释：删除分类
     * 创建人：Jack
     * 创建时间：2023/8/23 15:39
     * @param int $id
     * @return void
     */
    public function delete(int $id)
    {
        $count = $this->dao->getCount(['pid' => $id]);
        if ($count) {
            throw new AdminException('请先删除下级分类!');
        } else {
            $res = $this->dao->delete($id);
            if (!$res) throw new AdminException('请先删除下级分类!');
        }
    }

}
